<style>
.pure_table_extra { width: 1100px; margin-top: 5px; }
.pure_table_extra th { height: 25px; line-height: 25px; cursor: pointer; }
.pure_table_extra img { cursor: pointer; margin-right: 2px; }
.pure_table_extra td { vertical-align: middle; }
.pure_table_extra tr.cancel td { text-decoration: line-through; }
.ui-autocomplete-loading { background: #fff url('../3rd_party/jquery/images/gg_load.gif') no-repeat right center; }
</style>

<?php 
$t = Utils::get_param('t', '');
$t2 = Utils::get_param('t2', '');
$o = Utils::get_param('o', 'id');
$q = Utils::get_param('q', 'DESC');
$pg = Utils::get_param('pg', 1);

$opts = new stdClass();
$opts->cd = 't.track_id LIKE :v1 AND t.student_title__ LIKE :v2';
$opts->cdv = array(':v1' => '%' . $t . '%', ':v2' => '%' . $t2 . '%');
$data = $orm->get('pb_colg_enrollment', $o, $q, $pg, SIZE, $opts);
?>

<div class="pure-form">
  <input style="display: none;" id="keywords" type="text" placeholder="{Enrollment ID}"  onkeypress="if(event.which == 13) search(1, 'id', 'DESC');">
  <input id="keywords2" type="text" placeholder="{Student}"  onkeypress="if(event.which == 13) search(1, 'id', 'DESC');">
  <button onclick="search(1, 'id', 'DESC');" class="pure-button">Search</button>
</div>

<table class="pure-table pure_table_extra" cellpadding=0 cellspacing=0>
  <thead>
    <tr>
      <th style="display: none;width: 80px;" class="ui-widget-header" onclick="return search(1, 'track_id', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Enrollment ID</th>
      <th style="width: 40px;" class="ui-widget-header" onclick="return search(1, 'is_paid', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Status</th>
      <th style="width: 120px;" class="ui-widget-header" onclick="return search(1, 'student_id', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Student</th>
      <th style="width: 40px;" class="ui-widget-header" onclick="return search(1, 'program_type', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Type</th>
      <th class="ui-widget-header" onclick="return search(1, 'program_id', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Program</th>
      <th style="width: 60px;" class="ui-widget-header" onclick="return search(1, 'start_academic', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">From</th>
      <th style="width: 60px;" class="ui-widget-header" onclick="return search(1, 'end_academic', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">To</th>
      <th style="width: 80px;" class="ui-widget-header" onclick="return search(1, 'user_id', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">User</th>
      <th style="width: 60px;" class="ui-widget-header" onclick="return search(1, 'date', (__data && __data.q == 'ASC') ? 'DESC' : 'ASC');">Date</th>
      <th style="width: 40px;" class="ui-widget-header">&nbsp;</th>
    </tr>
  </thead>
  <tbody id="enrls_body"></tbody>
</table>
<div>&nbsp;</div>
<div id="pagination"></div>

<?php include '../include/colg/inc.enrl.js.php'; ?>

<script type="text/javascript">
  /*<![CDATA[*/
  var __dir = '<?php echo PLGN_DIR . basename(dirname(__FILE__)); ?>';
  var __pturl = '<?php echo ColgUtils::get_pturl($app_data); ?>';
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __file = '<?php echo addslashes($app_data->param_file); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __date = '<?php echo $date; ?>';
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __language = '<?php echo PT_LANGUAGE; ?>';
  var __ncea = '<?php echo PT_NCEA; ?>';
  
  $(function() {
    $('#keywords').autocomplete({
      source: __requrl + 'autocomplete',
      minLength: 1,
      select: function(event, ui) {
        $('#keywords').val(ui.item.value);
        search(1, 'id', 'DESC');
      }
    });

    $('#keywords2').autocomplete({
      source: __requrl + 'autocomplete2',
      minLength: 1,
      select: function(event, ui) {
        $('#keywords2').val(ui.item.value);
        search(1, 'id', 'DESC');
      }
    });

    $('#keywords').on('focus', function() {
      if (this.value == '') {
        this.value = '#';
      }
    });
    
    render();
  });
  
  function render() {
    $('#pagination').pagination({
      items: __data ? __data.t : 0,
      itemsOnPage: __data ? __data.s : 0,
      currentPage: __data ? __data.p : 0,
      cssStyle: 'compact-theme',
      onPageClick: function(pageNumber, event) {
        search(pageNumber, __data.o, __data.q);
      }
    });
    
    render_data();
  };
  
  function render_data() {
    if (__data) {
      Url.insert_param('pg', __data.p);
      Url.insert_param('o', __data.o);
      Url.insert_param('q', __data.q);
    }
    
    $('#enrls_body').empty();
    if (__data == null) {
      $('#enrls_body').append('<tr class="pure-table-even"><td colspan=10>Search for data</td></tr>');
      return;
    }
    for (var idx in __data.d) {
      var itm = __data.d[idx];
      $('#enrls_body').append(
        '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + ((itm.is_cancel == 0) ? '' : ' cancel') + '">' + 
          '<td style="display: none;">' + itm.track_id + '</td>' +
          '<td>' + (itm.is_paid == 0 ? 'Unpaid' : ((itm.balance__ < 0) ? '<span style="color: red">Owing</span>' : 'Paid')) + '</td>' +
          '<td title="' + itm.student_title__ + '">' + Utils.get_short(itm.student_title__, 15) + '</td>' +
          '<td>' + itm.program_type + '</td>' +
          '<td title="' + itm.program_name + '">' + Utils.get_short(itm.program_name, 60) + '</td>' +
          '<td>' + itm.start_academic + '</td>' +
          '<td>' + itm.end_academic + '</td>' +
          '<td>' + itm.user_title__ + '</td>' +
          '<td>' + itm.date + '</td>' +
          '<td>' + 
            '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-pencil" onclick="edit_enrl(' + itm.id + ');"></span>' +
            ((itm.is_paid == 1) ? '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-print" onclick="print_enrl(' + itm.id + ');"></span>' : '') +
          '</td>' + 
        '</tr>'
      );
    }
    if (__data.d.length == 0) {
      $('#enrls_body').append('<tr class="pure-table-even"><td colspan=10>None</td></tr>');
    }
  };

  function print_enrl(id) {
    var obj = Utils.get_by_id(id, __data.d);
    var tmpl = 'd_cop';
    if (obj.program_type == __language) {
      tmpl = 'l_cop';
    } else if (obj.program_type == __ncea) {
      tmpl = 'n_cop';
    }
    window.open(__pturl + '&tmpl=' + tmpl + '&id=' + id);
  };

  function search(pg, o, q) {
    if ($('#keywords').val() == '#') {
//      return;
    }
    var req = {
      term: $('#keywords').val(),
      term2: $('#keywords2').val(),
      pg: pg,
      q: q,
      o: o,
    }
    Ajax.request(
      __requrl + 'search',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data = rslt;
        render();
      },
      null
    );
    return false;
  };

  function edit_enrl(id) {
    var func = function() {
      var obj = Utils.get_by_id(id, __data.d);
      Ajax.request(
        __requrl + ((obj.is_paid == 0) ? 'submit_enrl' : 'clearup'),
        'req=' + Utils.encodeURI(JSON.stringify(obj)),
        function(rslt) {
          var obj = Utils.get_by_id(id, __data.d);
          obj.is_cancel = 0;
          obj.is_paid = (obj.is_paid == 0) ? 1 : 0;
          obj.balance__ = ((rslt) ? rslt.balance__ : 0);
          render_data();
          $('#dialog_enrl').dialog('close');
        },
        { confirm: 1 }
      );
    };

    var obj = Utils.get_by_id(id, __data.d);
    if (obj.is_paid == 1) {
      Ajax.request(
        __requrl + 'get_fees',
        'req=' + Utils.encodeURI(obj.id),
        function(rslt) {
          obj.__fees = rslt;
          $('#dialog_enrl').dialog({
            buttons: {
              'Clear up': function() {
                func();
              },
              'Cancel': function() { 
                $(this).dialog('close');
              }
            },
            width: 1200
          });
          ____enrl(obj, { m: 'pay' });
        },
        null
      );
    } else {
      $('#dialog_enrl').dialog({
        buttons: {
          'OK': function() {
            func();
          },
          'Cancel': function() { 
            $(this).dialog('close');
          }
        },
        width: 1200
      });
      ____enrl(obj, { m: 'pay' });
    }
  };
  /*]]>*/
</script>